package co.yixiang.modules.shop.rest;

import co.yixiang.aop.log.Log;
import co.yixiang.modules.shop.domain.YxShopSupplier;
import co.yixiang.modules.shop.service.YxShopSupplierService;
import co.yixiang.modules.shop.service.dto.YxShopSupplierQueryCriteria;
import co.yixiang.utils.OrderUtil;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @author Walert
* @date 2020-03-23
*/
@Api(tags = "供应商接口API管理")
@RestController
@RequestMapping("/api/yxShopSupplier")
public class YxShopSupplierController {

    private final YxShopSupplierService yxShopSupplierService;

    public YxShopSupplierController(YxShopSupplierService yxShopSupplierService) {
        this.yxShopSupplierService = yxShopSupplierService;
    }

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('admin','yxShopSupplier:list')")
    public void download(HttpServletResponse response, YxShopSupplierQueryCriteria criteria) throws IOException {
        yxShopSupplierService.download(yxShopSupplierService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询供应商接口API")
    @ApiOperation("查询供应商接口API")
    @PreAuthorize("@el.check('admin','yxShopSupplier:list')")
    public ResponseEntity<Object> getYxShopSuppliers(YxShopSupplierQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(yxShopSupplierService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增供应商接口API")
    @ApiOperation("新增供应商接口API")
    @PreAuthorize("@el.check('admin','yxShopSupplier:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody YxShopSupplier resources){
        resources.setAddTime(OrderUtil.getSecondTimestampTwo());
        return new ResponseEntity<>(yxShopSupplierService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改供应商接口API")
    @ApiOperation("修改供应商接口API")
    @PreAuthorize("@el.check('admin','yxShopSupplier:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody YxShopSupplier resources){
        resources.setChangeTime(OrderUtil.getSecondTimestampTwo());
        yxShopSupplierService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除供应商接口API")
    @ApiOperation("删除供应商接口API")
    @PreAuthorize("@el.check('admin','yxShopSupplier:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody Integer[] ids) {
        yxShopSupplierService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}